package com.jielin.handling.service.information.impl;

import com.alibaba.fastjson.JSONObject;
import com.jielin.handling.entity.information.Information;
import com.jielin.handling.entity.tsuspect.TSuspec;
import com.jielin.handling.mapper.information.InformationMapper;
import com.jielin.handling.mapper.tsuspect.TSuspecMapper;
import com.jielin.handling.service.information.InformationService;
import com.jielin.handling.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * Created by wangjienice on 2019/3/20.
 */
@Service
@Slf4j
public class InformationServiceImpl implements InformationService {
    @Autowired
    InformationMapper informationMapper;

    @Autowired
    TSuspecMapper tSuspecMapper;
    @Override
    public int addInformation(@RequestBody  Information information) {
        log.info("information:{}", JSONObject.toJSONString(information));
        information.setId(StringUtil.generatorUUID());
        TSuspec tSuspec=new TSuspec();
        tSuspec.setId(information.getSuspectId());
        TSuspec suspectState = tSuspecMapper.selectStatue(information.getSuspectId());
        if(!"8".equals(suspectState.getSuspectState())) {
            tSuspec.setSuspectState("4");
        }else {
            tSuspec.setSuspectState(suspectState.getSuspectState());
        }
        try {

            tSuspecMapper.updateState(tSuspec);

            return informationMapper.insert(information);
        }catch (Exception e){
            log.info("service 事务回滚:{}",e);
            //抛出异常让其回滚
            throw new RuntimeException("事务回滚");
        }
    }
}
